﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.NodeFactory;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
using System.Xml;
using umbraco.DataLayer;

namespace ExtendedWorkFlow.DataTypes
{

    [ToolboxData("<{0}:UserRoles runat=server></{0}:UserRoles>")]
    public class UserRoles : ListBoxMoveBase
    {

        #region Variables

        int _Id;

        #endregion

        #region Constructor

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            this.EnsureChildControls();

            this.AddClicked += new EventHandler(_Add_Click);
            this.RemoveClicked += new EventHandler(_Remove_Click);
        }

        #endregion

        #region Child Controls

        protected override void ChildControlsCreated()
        {
            if (this.Page.Request.QueryString["id"] != null)
                this._Id = Convert.ToInt32(this.Page.Request.QueryString["id"]);

            if (!this.Page.IsPostBack)
            {
                this.SetInitalUserRoles();
                this.SetupNodeUserRoles();
            }
        }

        #endregion

        #region User Roles

        void SetInitalUserRoles()
        {
            using (IRecordsReader objReader = Helpers.WorkFlowHelper.GetUserRoles())
            {
                this.Source.DataSource = objReader;
                this.Source.DataTextField = "name";
                this.Source.DataValueField = "uid";
                this.Source.DataBind();
            }
        }

        void SetupNodeUserRoles()
        {
            if (this._Id != 0)
            {
                using (IRecordsReader objReader = Helpers.WorkFlowHelper.GetUserRoles(this._Id))
                {
                    this.Destination.DataSource = objReader;
                    this.Destination.DataTextField = "name";
                    this.Destination.DataValueField = "uid";
                    this.Destination.DataBind();
                }
            }
        }

        #endregion

        #region Button Postbacks

        void _Remove_Click(object sender, EventArgs e)
        {
            List<ListItem> colRemove = new List<ListItem>();

            if (this.Destination.SelectedItem != null)
            {
                // Loop though all selected items
                foreach (ListItem objListItem in this.Destination.Items)
                    if (objListItem.Selected)                   
                        colRemove.Add(objListItem);

                foreach (ListItem objRemove in colRemove)
                    this.Destination.Items.Remove(objRemove);
            }
        }

        void _Add_Click(object sender, EventArgs e)
        {
             if (this.Source.SelectedItem != null)
                foreach (ListItem objListItem in this.Source.Items)
                    if (objListItem.Selected &&
                        this.Destination.Items.FindByValue(objListItem.Value) == null)
                        this.Destination.Items.Add(new ListItem(objListItem.Text, objListItem.Value));
        }

        #endregion

        #region Saving

        public void Save()
        {
            if (this._Id != 0 &&
                this.Destination != null)
            {
                ExtendedWorkFlow.Helpers.WorkFlowHelper.RemoveUserRolesFromNode(this._Id);

                if (this.Source.SelectedItem != null)
                {
                    // Loop though all selected items
                    foreach (ListItem objListItem in this.Source.Items)
                        if (objListItem.Selected)
                        {
                            string[] arrData = objListItem.Value.Split(":".ToCharArray());

                            // Add the user or role to the node
                            Helpers.WorkFlowHelper.AddUserRoleToNode(
                                this._Id,
                                Convert.ToInt32(arrData[0]),
                                arrData[1]);
                        }
                }
            }
        }

        #endregion

    }

}